/*
MIT Licence allows free use in all software (including GPL and commercial)

The licence text below is the boilerplate "MIT Licence" used from:
http://www.opensource.org/licenses/mit-license.php

Copyright (c) 2012, An Pham

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "stdafx.h"
#include <assert.h>
#include <winreg.h>
#include <string>
#include "MFLinkToolUtil.h"
#include "MFLinkToolReg.h"

using namespace std;

Registry::Registry()
{
	AccessMode = KEY_ALL_ACCESS;
	Changed = false;	
	CurrentPath = "";
	LazyWrite = true;
	TrimString = true;
	Key = 0;
	RootKey = HKEY_CURRENT_USER;
	Wow64 = 0;
}

Registry::~Registry()
{
	closeKey();
}

void Registry::closeKey()
{
	if (Key)
	{
		if (!LazyWrite && Changed)
			RegFlushKey(Key);
		RegCloseKey(Key); 
		Key = 0;
	}
	Changed = false;
}

bool Registry::deleteKey(const string& AKey)
{
	assert(AKey.length() > 0 && AKey[0] != '\\');

	closeKey();

    if (!keyExists(AKey)) 
		return true;

	if (RegDeleteKeyA(RootKey, AKey.c_str()) != ERROR_SUCCESS) 
		return false;

	return true;
}

bool Registry::deleteValue(const string& AName)
{
	assert(Key != 0);

	if (0 == Key)
		return false;

	if (RegDeleteValueA(Key, AName.c_str()) != ERROR_SUCCESS)
		return false;
	else
	{
		Changed = true;
		return true;
	}
}

bool Registry::keyExists(const string& AKey)
{
	assert(AKey.length() > 0 && AKey[0] != '\\');

	HKEY LocalKey = 0;
	if (RegOpenKeyExA(RootKey, AKey.c_str(), 0, KEY_READ | Wow64, &LocalKey) != ERROR_SUCCESS)
		return false;
	RegCloseKey(LocalKey);
	return true;
}

bool Registry::openKey(const string& AKey, const bool ACanCreate)
{
	assert(AKey.length() > 0 && AKey[0] != '\\');

	closeKey();

	if (ACanCreate) 
    {
		DWORD Disposition = 0;
		if (RegCreateKeyExA(RootKey, AKey.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, AccessMode | Wow64, NULL, &Key, &Disposition) != ERROR_SUCCESS) 
			return false;
        CurrentPath = AKey;
		if (REG_CREATED_NEW_KEY == Disposition) 
			Changed = true;
		return true;
    }

	if (RegOpenKeyExA(RootKey, AKey.c_str(), 0, AccessMode | Wow64, &Key) != ERROR_SUCCESS) 
		return false;

    CurrentPath = AKey;
	return true;
}

/*
OleDateTime Registry::readDateTime(const string& AName, const OleDateTime ADefault)
{
	assert(Key != 0);

	OleDateTime Data;
	DWORD Type = REG_BINARY;
	DWORD Size = sizeof(Data);

	if (RegQueryValueExA(Key, AName.c_str(), NULL, &Type, (LPBYTE)&Data, &Size) != ERROR_SUCCESS) 
		return ADefault;

	return Data;
}
*/

double Registry::readFloat(const string& AName, const double ADefault)
{
	assert(Key != 0);

	double Data = ADefault;
	DWORD Type = REG_BINARY;
	DWORD Size = sizeof(Data);

	if (RegQueryValueExA(Key, AName.c_str(), NULL, &Type, (LPBYTE)&Data, &Size) != ERROR_SUCCESS) 
		return ADefault;

	return Data;
}

double Registry::readInt(const string& AName, const int ADefault)
{
	assert(Key != 0);

	int Data = ADefault;
	DWORD Type = REG_BINARY;
	DWORD Size = sizeof(Data);

	if (RegQueryValueExA(Key, AName.c_str(), NULL, &Type, (LPBYTE)&Data, &Size) != ERROR_SUCCESS) 
		return ADefault;

	return Data;
}

string Registry::readString(const string& AName, const string ADefault)
{
	assert(Key != 0);

	char Data[8192];
    DWORD Type = REG_SZ;
	DWORD Size = sizeof(Data);

	Type = valueDataType(AName);
	if (Type != REG_SZ && Type != REG_EXPAND_SZ && Type != REG_MULTI_SZ)
		return ADefault;

	if (RegQueryValueExA(Key, AName.c_str(), NULL, &Type, (LPBYTE)Data, &Size) != ERROR_SUCCESS)
		return ADefault;

	if (0 == Size)
		return "";

	string S(Data, Size - 1); // -1 for removing null char
	if (TrimString)
		stringTrim(S);
	return S;
}

bool Registry::keyInfo(KeyInfo& Info)
{
	assert(Key != 0);

	memset(&Info, 0, sizeof(Info));

	if (0 == Key)
		return false;

	Info.ClassNameLength = sizeof(Info.ClassName);
	return (ERROR_SUCCESS == RegQueryInfoKeyA(Key, Info.ClassName, &Info.ClassNameLength,
				NULL, &Info.SubKeyCount, &Info.MaxSubKeyName, NULL, &Info.ValueCount,
				&Info.MaxValueName, &Info.MaxValueLength, NULL, &Info.LastWritten)) ;
}

int Registry::subKeyCount()
{
	assert(Key != 0);

	KeyInfo Info;
	if (keyInfo(Info))
		return (int)Info.SubKeyCount;
	else
		return -1;
}

vector<string> Registry::subKeyNames()
{
	assert(Key != 0);

	vector<string> Names;

	const int Count = subKeyCount();
	for (int i = 0; i < Count; i++)
	{
		char Name[1024];
		DWORD Len = sizeof(Name);
		if (ERROR_SUCCESS == RegEnumKeyExA(Key, i, Name, &Len, NULL, NULL, NULL, NULL))
			Names.push_back(Name);
	}

	return Names;
}

int Registry::valueDataSize(const string& AName)
{
	assert(Key != 0);

	if (0 == Key)
		return -1;

    DWORD Size = 1;
	if (RegQueryValueExA(Key, AName.c_str(), NULL, NULL, NULL, &Size) != ERROR_SUCCESS)
		return -1;
	return (int)Size;
}

int Registry::valueDataType(const string& AName)
{
	assert(Key != 0);

	if (0 == Key)
		return -1;

    DWORD Type = 1;
	if (RegQueryValueExA(Key, AName.c_str(), NULL, &Type, NULL, NULL) != ERROR_SUCCESS)
		return -1;
	return (int)Type;
}

bool Registry::valueExists(const string& AName)
{
	assert(Key != 0);

	if (0 == Key)
		return false;

	if (RegQueryValueExA(Key, AName.c_str(), NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
		return false;
	else
		return true;
}

int Registry::valueCount()
{
	assert(Key != 0);

	KeyInfo Info;
	if (keyInfo(Info))
		return (int)Info.ValueCount;
	else
		return -1;
}

vector<string> Registry::valueNames()
{
	assert(Key != 0);

	vector<string> Names;

	const int Count = valueCount();
	for (int i = 0; i < Count; i++)
	{
		char Name[1024];
		DWORD Len = sizeof(Name);
		if (ERROR_SUCCESS == RegEnumValueA(Key, i, Name, &Len, NULL, NULL, NULL, NULL))
			Names.push_back(Name);
	}

	return Names;
}

/*
bool Registry::writeDateTime(const string& AName, const OleDateTime AValue)
{
	assert(Key != 0);

	if (0 == Key)
		return false;

	if (RegSetValueExA(Key, AName.c_str(), 0, REG_BINARY, (LPBYTE)&AValue, sizeof(AValue)) != ERROR_SUCCESS) 
		return false;

	Changed = true;
	return true;
}
*/

bool Registry::writeFloat(const string& AName, const double AValue)
{
	assert(Key != 0);

	if (0 == Key)
		return false;

	if (RegSetValueExA(Key, AName.c_str(), 0, REG_BINARY, (LPBYTE)&AValue, sizeof(AValue)) != ERROR_SUCCESS) 
		return false;

	Changed = true;
	return true;
}

bool Registry::writeInt(const string& AName, const int AValue)
{
	assert(Key != 0);

	if (0 == Key)
		return false;

	if (RegSetValueExA(Key, AName.c_str(), 0, REG_BINARY, (LPBYTE)&AValue, sizeof(AValue)) != ERROR_SUCCESS) 
		return false;

	Changed = true;
	return true;
}

bool Registry::writeString(const string& AName, const string& AValue)
{
	assert(Key != 0);

	if (0 == Key)
		return false;

	if (RegSetValueExA(Key, AName.c_str(), 0, REG_SZ, (LPBYTE)AValue.c_str(), (DWORD)(AValue.length() + 1)) != ERROR_SUCCESS)
		 return false;

	Changed = true;
	return true;
}
